import { asyncRouterMap, constantRouterMap } from '@/router'
// asyncRouterMap这是一个导出的404
import { hasPermissionOfRouteName } from '@/utils/permission'

const permission = {
    state: {
        routers: constantRouterMap, // 合并之后的路由
        addRouters: [] // 动态路由
    },
    mutations: {
        SET_ROUTERS: (state, routers) => {
            state.addRouters = routers // 这是添加动态路由
            // constantRouterMap这是所有的静态路由
            state.routers = constantRouterMap.concat(routers) // 这是与动态路由合并
        }
    },
    actions: {
        GenerateRoutes({ commit }, data) {
            console.log(data, '-=--=-=-=-=')
            return new Promise(resolve => {
                // / 动态路由
                // / 内部函数递归路由 筛选需要的数据
                function filterAsyncRouter(routers) {
                    // 这个是我自己的路由
                    return routers.filter(route => {
                        // route是全部的权限点
                        if (hasPermissionOfRouteName(route.name)) {
                            if (route.children && route.children.length) {
                                route.children = filterAsyncRouter(route.children)
                            }
                            return true
                        } else {
                            return false
                        }
                    })
                }
                const accessedRouters = filterAsyncRouter(asyncRouterMap)
                commit('SET_ROUTERS', accessedRouters)
                // commit('SET_ROUTERS', asyncRouterMap)
                resolve()
            })
        }
    }
}

export default permission
